var appPath = getAppPath();

$(function(){
	initJspGrid();
});


// 一键秀分类管理【YJX】
// 相当于 一键秀的大分类
function initJspGrid(){
	$('#tmplType').datagrid({
	    url:appPath + "/yjxTmplType/getTmplTypeInfoPg.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'st_id',title:'分类ID',width:25,align:'center',sortable:false},
	        {field:'parent',title:'父ID',width:25,align:'center',sortable:false},
		    {field:'edit',title:'编辑',width:30,align:'center',sortable:false,
					formatter:function(value,rowData,rowIndex){
						var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' ";
						ret += " onClick='editTmpl("+rowData.st_id+")'/></a>";
						return ret;
					}
			},
	        {field:'title',title:'分类名称',width:60,align:'left',sortable:false},
	        {field:'desc',title:'用途简介',width:70,align:'left',sortable:false,
	        	formatter:function(desc, rowData, rowIdx){
	        		var ret = rowData.desc;
	        		if(ret && ret.length>10){
	        			return ret.substr(0,10)+'...';
	        		}
	        		return ret;
	        	}
	        },
	        {field:'order',title:'排序',width:20,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return "<a href='javascript:void(0)' onClick='editOrder("+rowData.st_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		    },
	        {field:'publish',title:'发布状态',width:35,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<spna style="color:green;">已发布</span>';
	        		return '<span style="color:red;">未发布</span>';
	        	}
	        },
	        {field:'ver',title:'版本号',width:20,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		return '<span style="color:black;">'+value+'</span>';
	        	}
	        },
	        {field:'design', title:'内容', width:40, align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		return "<a href='javascript:void(0)' onclick='ShowTmplSet(\""+rowData.title+"\","+rowData.st_id+",\""+rowData.ver+"\")'>点击设置</a>";
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:70,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[{
				text:'添加分类',
				iconCls:'icon-add',
				handler:function(){
					addTmplType();
				}
			},
			'-',
			{
				text:'发布分类',
				iconCls:'icon-redo',
				handler:function(){
					doPublish();
				}
			},
			'-',
			{
				text:'取消发布',
				iconCls:'icon-cancel',
				handler:function(){
					cancelPublish();
				}
			},
			'-',
			{
				text:'删除分类',
				iconCls:'icon-no',
				handler:function(){
					delTmplType();
			}
			}]
	});
}

// 展示分类下的模板组
function ShowTmplSet(title,id,ver){
	$("#st_id").val(id);
	$("#tsId").html(id);
	$("#stTitle").html(title);
	$("#stVer").html(ver);
	$('#tDesignDlg').window({
		title:'分类下的模板组列表',
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#tDesignDlg').window('open');
	$('#tDesignDlg').window('move',{top:20,left:20});
	initTmplSetGird();
}

/* * The new classification
 */
function addTmplType(){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#tmpImg").html('').val('');
	$("#editTmplUse").combobox('select',301).combobox('enable');
	$("#editBtn").show();
	$('#editDlg').window({
		title:'新建分类',
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}

/* Click to open the edit template group list
 */
function editTmpl(stId){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#editStId").val(stId);
	
	var url = appPath + "/yjxTmplType/getATmplType.action?stId="+ stId;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			$("#editTmplTitle").val(info.title||'');
			$("#editTmplDesc").val(info.desc||'');
			$("#editTmplVer").val(info.ver||'');
		}
	});
	
	$("#editBtn").show();
	$('#editDlg').window({
		title:'编辑模板组',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}

function cancelEdit(){
	$('#editDlg').window('close');
}

/* 	提交新建/修改分类
 */
function doEdit(){
	var stId = $("#editStId").val();
	var title = $("#editTmplTitle").val();
	var desc = $("#editTmplDesc").val();
	var ver = $("#editTmplVer").val(); // 版本号
	
	if(title && title.length>0) {
		var url = appPath+"/yjxTmplType/addTmplType.action"
		var sendData = {
			"showType.st_id": stId||"",
			"showType.title": title || "",
			"showType.desc": desc || "",
			"showType.ver": ver || ""
		};
	    $.post(url, sendData, function(data){  
			if (data == "ok") { 
				$.messager.alert('提示信息','提交成功','info');
				$('#editDlg').window('close');
				doQuery();
	        } else {
	        	$.messager.alert('提示信息','提交失败','info');
	        }
	    });
	    
	}else{
		$.messager.alert('提示信息','请输入分类名称','info');
	}
	
}

// 删除分类
function delTmplType(){
	var selectItem=$("#tmplType").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择分类！','warning');
		return;
	}
	for(var i=0; i<selectItem.length; i++){
		if(Number(selectItem[i].publish||'0')==1){
			$.messager.alert('提示信息','不能删除已发布的分类！','warning');
			return;
		}
	}
	
	$.messager.confirm("确认",
		"是否确认[删除]所选分类？<br/><span style='color:red'>*对应的[分类/分类关联信息]将一并删除!</span>",
		function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids = ids + item.st_id + ",";
				});
				var url = appPath+"/yjxTmplType/delTmplType.action?ids="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','删除成功','info');
						doQuery();
					}else{
						$.messager.alert('提示信息','删除失败','info');
					}
				});
	        }
		}
	);
}

// 发布分类
function doPublish(){
	var selectTmplS=$("#tmplType").datagrid("getChecked");
	if(!selectTmplS || selectTmplS.length == 0){
		$.messager.alert('提示信息','请选择分类处理！','warning');
		return;
	}else if(selectTmplS.length>1){
		$.messager.alert('提示信息','请选择一个分类处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmplS, function(index, item){
		if(item.publish != 1 && item.publish != '1'){
			doFlg = true;
			ids = ids + item.st_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择未发布的分类处理！','warning');
		return;
	}
	
	$.messager.confirm("确认",
		"是否确认[发布]分类？<br/><span style='color:red'>*请确保已经过认真检查!</span>",
		function (r) {
	        if (r) {
	        	var url = appPath+"/yjxTmplType/pubTmplType.action?ids="+ids;
	        	$.post(url,function(data){
	        		if(data=="ok"){
	        			$.messager.alert('提示信息','模板组发布成功！','info');
	        			doQuery();
	        		} else if (data=="ng"){
	        			$.messager.alert('提示信息','NG!','warning');
	        		}
	        	});
	        }
		}
	);
}

// 取消发布分类
function cancelPublish(){
	var selectTmplS=$("#tmplType").datagrid("getChecked");
	if(!selectTmplS || selectTmplS.length == 0){
		$.messager.alert('提示信息','请选择已发布的模板组进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmplS, function(index, item){
		if(item.publish == 1 || item.publish == '1'){
			doFlg = true;
			ids = ids + item.st_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择已发布的模板组进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认", "是否确认[取消]已发布模板组？", function (r) {
		if(r){
			var url = appPath+"/yjxTmplType/cancelPubTmplType.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','模板组已取消发布！','info');
					doQuery();
				}
			});
		}
	});
}

/** 显示顺序*/
function editOrder(tsId, order){
	$("#orderStId").val(tsId);
	$("#order").val(order);
	$("#orderBtn").show();
	$('#orderDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#orderDlg').window('move',{top:20,left:20});
	$('#orderDlg').window('open');
}

/**修改显示顺序*/
function doUpdOrder(){
	var stId = $("#orderStId").val();
	var order = $("#order").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	var url = appPath+"/yjxTmplType/upTmplTyptOrder.action?stId="+stId+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#orderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}

/** 结束显示顺序编辑*/
function cancelUpdOrder(){
	$('#orderDlg').window('close');
}

/**获取请求信息
 * */
function getQueryParam(){
	var queryObject = {};
	var findPubFlg = $("#queryPublish").combobox('getValue');//发布状态
	var findTypeTitle = $("#queryTypeTitle").val();
	var findTypeStId = $("#queryStId").val();
	if(findPubFlg && Number(findPubFlg||'-1')>=0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTypeTitle && findTypeTitle.length>0) queryObject.findTypeTitle = findTypeTitle;// 名称
	if(findTypeStId && findTypeStId.length>0) queryObject.findTypeStId = findTypeStId;// 分类组ID
	return queryObject;
}

/** 查询数据*/
function doQuery(){
	$("#tmplType").datagrid('unselectAll');
	$("#tmplType").datagrid('load', getQueryParam());
}

function doQuery2(startFlg){
	$("#tmplTypeRGrid").datagrid('unselectAll');
	if(startFlg==1){
		$("#tmplTypeRGrid").datagrid('load', getQueryParam());
	} else {
		$("#tmplTypeRGrid").datagrid('reload', getQueryParam());
	}
}

//加载 分类下的模板组
function initTmplSetGird(){
	var stId=$("#st_id").val();
	$('#tmplTypeRGrid').datagrid({
	    url:appPath + "/yjxTmplType/getTmplTypeRInfo.action?stId="+stId,
	    height:'410',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'r_id',title:'主键ID',width:25,align:'center',sortable:false},
	        {field:'st_id',title:'分类ID',width:25,align:'center',sortable:false},
	        {field:'set_id',title:'模板组ID',width:25,align:'center',sortable:false},
	        {field:'title',title:'模板组名称',width:40,align:'center',sortable:false},
	        {field:'desc',title:'模板组描述',width:40,align:'center',sortable:false},
		    {field:'publish',title:'发布状态',width:40,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
	        },
	        {field:'use',title:'用途',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = '普通分类';
	        		switch (value){
	        		case 0: ret = '普通分类';break;
	        		case 10: ret = '<font color="#CD3700">专题分类</font>';break;
	        		}
	        		return ret;
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:60,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:60,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true,
		toolbar:[
		{
			text:'<span style="color:#2F4F4F;">给分类添加模板组</span>',
			iconCls:'icon-redo',
			handler:function(){
				addTmplSet();// 给分类添加模板组
			}
		},
		{
			text:'删除分类下的模板组',
			iconCls:'icon-cancel',
			handler:function(){
				delTmplSet();
			}
		}]
	    
	});
}

//添加设计模板
function addTmplSet(){
	var setId = $('#st_id').val();
	$.messager.confirm('确定','是否确定添加模板组？',function(r){
		if(r){
			$("#desBtn").show();
			$("#sDesignForm").form('clear');
			$("#queryUse").combobox('select',-1);
			$('#tmplSetDlg').window({
				title:'模板组列表',
				iconCls:'icon-edit',
				modal: true,
				shadow: true,
				collapsible:false,
				minimizable:false,
				maximizable:false
			});
			$('#tmplSetDlg').window('move',{top:20,left:20});
			$('#tmplSetDlg').window('open');
			$("#queryTSPub").combobox({
				data:[{'id':"",'name':'不限'},{'id':"0",'name':'未发布'},{'id':"1",'name':'已发布'}],
				valueField:'id',
				textField:'name'
			});
			initTmplSetGrid();
		}
	});
}

//移除设计模板
function delTmplSet(){
	var selectItem=$("#tmplTypeRGrid").datagrid("getChecked");
	var type = $("#stType").val();
	var setId = $("#st_id").val();
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择一个模板组！！','warning');
		return;
	}
	$.messager.confirm("确认","确认移除该模板组？",function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids= ids + item.r_id+",";
				});
				var url = appPath+"/yjxTmplType/delTmplTypeTS.action?rId="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','移除成功','info');
						doQuery2();
					}else{
						$.messager.alert('提示信息','移除失败','info');
					}
				});
	        }
		}
	);
}

// 模板组列表哦
// 模板组管理【YJX】
// 初始化加载 模板组  模板组相当于小分类 分类管理相当于大分类
function initTmplSetGrid(){
	var stId = $('#st_id').val();
	$('#tmplSetGrid').datagrid({
	    url:appPath + "/yjxTmplType/getTmplSetInfoPg.action?stId="+stId,
	    height:'420',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam3(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'isChk',title:'状态',width:30,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if (value){
	        			return '<span style="color:red;">已添加</span>';
	        		}
	        		return '<span style="color:green;">未添加</span>';
	        	}
	        },
	        {field:'set_id',title:'模板组ID',width:30,align:'center',sortable:false},
	        {field:'title',title:'编组名称',width:60,align:'left',sortable:false},
	        {field:'desc',title:'用途简介',width:70,align:'left',sortable:false,
	        	formatter:function(desc, rowData, rowIdx){
	        		var ret = rowData.desc;
	        		if(ret && ret.length>10){
	        			return ret.substr(0,10)+'...';
	        		}
	        		return ret;
	        	}
	        },
	        {field:'order',title:'排序',width:20,align:'center',sortable:false},
	        {field:'publish',title:'发布状态',width:35,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<spna style="color:green;">已发布</span>';
	        		return '<span style="color:red;">未发布</span>';
	        	}
	        },
	        {field:'use',title:'用途',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = '普通分类';
	        		switch (value){
	        		case 0: ret = '普通分类';break;
	        		case 10: ret = '<font color="#CD3700">专题分类</font>';break;
	        		}
	        		return ret;
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:70,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
	});
}


function addToTmplSr(){
	var checkRows = $("#tmplSetGrid").datagrid('getChecked');
	var stId = $("#st_id").val();
	if(checkRows.length==0){
		$.messager.alert('提示信息','请选择模板组！','info');
		return;
	}else{
		var ids ='';
		var info = [];
		for(var i=0;i<checkRows.length;i++){
			var st = Number(checkRows[i].isChk);//根据是否ts_id>0  判断是否已经添加过模板
			if(st<=0){
				 ids = ids + checkRows[i].set_id+',';
			}else{
				info.push(checkRows[i].set_id);
			}
		}
		if(info.length>0){
			$.messager.alert('提示信息','勾选的有已经有已经添加过的模板组！','info');
			return;
		}
		$.messager.confirm("确认","确定将该模板组添加到该分类下？",function(r){
	        if (r) { 
				var url = appPath+"/yjxTmplType/addSuitTmplSet.action";
	        	var data = {"stId":stId,"set_id":ids};
				$.post(url,data,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','添加成功！','info');
					}else if(data=="exit"){
						$.messager.alert('提示信息','该模板组下已经存在该设计模板！！','info');
					}else if(data=="no"){
						$.messager.alert('提示信息','添加失败','info');
					}
					doQuery2();
					doQuery3();
				});
	        }
		});
	}
}

/**获取请求信息*/
function getQueryParam3(){
	var queryObject = {};
	var findTSPub = $("#queryTSPub").combobox('getValue'); // 模板组发布状态
	var findTSName=$("#queryTSName").val();
	var findTSId=$("#queryTSId").val();
	var findUse = $("#queryUse").combobox('getValue');
	
	if (Number(findUse)>=0)queryObject.findUse = findUse;
	if(findTSPub&&findTSPub.length>0&&Number(findTSPub)>=0) queryObject.findTSPub = findTSPub;// 模板组发布状态
	if(findTSName&&findTSName.length>0) queryObject.findTSName = findTSName;// 模板组名称
	if(findTSId&&findTSId.length>0) queryObject.findTSId = findTSId;// 模板组ID
	return queryObject;
}

/**查询数据*/
function doQuery3(startFlg){
	$("#tmplSetGrid").datagrid('unselectAll');
	if(startFlg == 1) {
		$("#tmplSetGrid").datagrid('load', getQueryParam3());
	} else {
		$("#tmplSetGrid").datagrid('reload', getQueryParam3());
	}
}